//
//  OneChatService.m
//  lTTheOne
//
//  Created by AA on 2024/3/10.
//

#import "OneChatService.h"
#import <oneCommonLib/UMLogFilesManager.h>

@interface OneChatService ()

@end

@implementation OneChatService

#define ParameterError @"请求参数错误"
#define ServerError @"服务器报错"

- (NSDictionary *)requstBody {
    return [NSDictionary dictionary];
}

- (void)headers {
    [PPNetworkHelper setValue:[NSString stringWithFormat:@""] forHTTPHeaderField:@""];
}

// 是否缓存需自行添加

- (void)requst:(RequestSuccessBlock)success fail:(RequestFailBlock)fail {
    if (_requstType == RequstPost) {
        // URL中带参数
        [self requstPostUrl:success fail:fail];

    } else if (_requstType == Requstget) {
        [self requstGetUrl:success fail:fail];
    }
}

- (void)requstPostUrl:(RequestSuccessBlock)success fail:(RequestFailBlock)fail {
    [self headers];

    if (![self domain]) {
        fail(nil, ParameterError);
        return;
    }

    if (![self port]) {
        fail(nil, ParameterError);
        return;
    }

    // 开始拼接URL
    NSString *requstURL = [NSString stringWithFormat:@"%@%@", [self domain], [self port]];
    [PPNetworkHelper cancelRequestWithURL:requstURL];    // 取消当前接口之前的请求
    NSDictionary *dic = [self requstBody];
    [PPNetworkHelper POST:requstURL
        parameters:dic
        success:^(id responseObject) {
        UMLog(@"InfoRet Url:%@",requstURL);
        
          [OneChatService InfoRet:responseObject
              success:^(id _Nullable responseObject) {
                success(responseObject);
              }
              fail:^(NSError *_Nullable error, NSString *_Nullable errorStr) {
                fail(error, errorStr);
                UMLog(@"One_requstURL:%@____errorStr:%@", requstURL, errorStr);
              }];
        }
        failure:^(NSError *error) {
          fail(error, ServerError);
          UMLog(@"One_requstURL:%@____error:%@", requstURL, error);
        }];
}

- (void)requstGetUrl:(RequestSuccessBlock)success fail:(RequestFailBlock)fail {
    [self headers];

    if (![self domain]) {
        fail(nil, ParameterError);
        return;
    }

    if (![self port]) {
        fail(nil, ParameterError);
        return;
    }

    //    [PPNetworkHelper setResponseSerializer:PPResponseSerializerHTTP];
    // 开始拼接URL
    NSString *requstURL = [NSString stringWithFormat:@"%@%@", [self domain], [self port]];
    NSDictionary *mydic = [self requstBody];
    [PPNetworkHelper GET:requstURL
        parameters:mydic
        success:^(id responseObject) {
        UMLog(@"InfoRet Url:%@",requstURL);
        
          [OneChatService InfoRet:responseObject
              success:^(id _Nullable responseObject) {
                success(responseObject);
              }
              fail:^(NSError *_Nullable error, NSString *_Nullable errorStr) {
                UMLog(@"One_requstURL:%@____errorStr:%@", requstURL, errorStr);
                fail(error, errorStr);
              }];
        }
        failure:^(NSError *error) {
          fail(error, ServerError);
          UMLog(@"One_requstURL:%@____error:%@", requstURL, error);
        }];
}

// 上传单张或多张图片
- (void)Uploadpicturesimages:(NSArray *)images success:(RequestSuccessBlock)success fail:(RequestFailBlock)fail {
    [self headers];

    if (![self domain]) {
        fail(nil, ParameterError);
        return;
    }

    if (![self port]) {
        fail(nil, ParameterError);
        return;
    }

    NSMutableArray *result = [NSMutableArray array];

    NSString *requstURL = [NSString stringWithFormat:@"%@%@", [self domain], [self port]];

    dispatch_group_t group = dispatch_group_create();
    for (NSInteger i = 0; i < images.count; i++) {
        dispatch_group_enter(group);

        NSDictionary *dic = [self requstBody];
        [PPNetworkHelper uploadImagesWithURL:requstURL
            parameters:dic
            name:@"user_icon"
            images:@[ images[i] ]
            fileNames:nil
            imageScale:0.5
            imageType:@"png"
            progress:^(NSProgress *progress) {

            }
            success:^(id responseObject) {
              UMLog(@"第 %d 张图片上传成功: %@", (int)i + 1, responseObject);
              //           NSDictionary *datadic =  [responseObject valueForKey:@"data"];
              ////           NSString *imagestr = [datadic valueForKey:@"path"];
              //           NSString *urlimagestr = [datadic valueForKey:@"path"];
              [result addObject:@"1"];
              dispatch_group_leave(group);
            }
            failure:^(NSError *error) {
              UMLog(@"第 %d 张图片上传失败: %@", (int)i + 1, error);
              [result addObject:@""];
              dispatch_group_leave(group);
            }];
    }

    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
      UMLog(@"上传完成!");
      NSError *error;
      if (result.count > 0) {
          if ([result containsObject:@""]) {
              fail(error, @"上传失败");
          } else {
              success(result);
          }
      } else {
          fail(error, @"上传失败");
      }
    });
}

// 请求码返回
+ (void)InfoRet:(id)responseObject success:(RequestSuccessBlock)success fail:(RequestFailBlock)fail {
    UMLog(@"InfoRet responseObject:%@",responseObject);
    
    if ([responseObject[@"code"] isKindOfClass:[NSNull class]]) {
        fail(nil, @"服务器返回的对象为空");
        return;
    }

    int code = [responseObject[@"code"] intValue];

    if (code == 200) {
        success(responseObject);
    } else if (code == 300) {
        if ([responseObject[@"msg"] isKindOfClass:[NSNull class]]) {
            fail(nil, @"错误，服务器为返回错误信息说明");
        } else {
            NSString *msg = responseObject[@"msg"];
            fail(nil, msg);
        }
    } else if (code == 101) {
        //        [NSObject clearISLogin];
        //        //        [ConLocModel isioding];
        //        fail(nil,@"登录失效");
        //
        //        if ([[self getCurrentVC] isKindOfClass:[TheOne_phoneLodingVC class]]==NO) {
        //            TheOne_phoneLodingVC *TheOne_loginvc = [[TheOne_phoneLodingVC alloc] init];
        //            TheOne_loginvc.hiddleCloseBtn = YES;
        //            EasyNavigationController *nav = [[EasyNavigationController alloc] initWithRootViewController:TheOne_loginvc];
        //            [UIApplication sharedApplication].keyWindow.rootViewController = nav;
        //        }

    } else if (code == 400) {
        fail(nil, @"系统逻辑业务出错");
    } else if (code == 102) {
        fail(nil, @"用户不存在");
    } else {
        fail(nil, @"服务器返回码不正确");
    }
}

@end
